<template>
  <div class="course-info">
    <van-cell-group :style="styleOptions">
      <!-- 课程图片 -->
      <van-cell class="course-img">
        <img :src="course.courseImgUrl"/>
      </van-cell>
      <!-- 课程描述 -->
      <van-cell class="course-description">
          <h2 v-text="course.courseName"></h2>
          <!-- 概述 -->
          <p v-text="course.previewFirstField"></p>
          <!-- 课程销售信息 -->
          <div class="course-sale-info">
              <p class="course-price">
                 <span class="discounts">￥{{course.discounts}}</span>
                 <span>￥{{course.price}}</span>
              </p>
              <span class="tag">{{ course.sales }}人已购</span>
              <span class="tag">每周三、周五更新</span>
          </div>
      </van-cell>
      <!-- 课程详细内容 -->
      <van-cell class="course-detail">
          <van-tabs sticky scrollspy>
            <van-tab title="详情">
              <div v-html="course.courseDescription"></div>
            </van-tab>
            <van-tab title="内容">
              <course-section v-for="item in sections" :key="item.id"
               :section-data="item">
              </course-section>
            </van-tab>
          </van-tabs>
      </van-cell>
    </van-cell-group>
    <!-- 底部支付功能 -->
    <van-tabbar v-if="!course.isBuy">
      <div class="price">
        <span v-text="course.discountsTag"></span>
        <span class="discounts">￥{{course.discounts}}</span>
        <span>￥{{course.price}}</span>
      </div>
      <van-button type="primary" @click="handlePay">立即购买</van-button>
    </van-tabbar>
  </div>
</template>

<script>
import { getCourseById, getSectionAndLesson } from '@/services/course'
import courseSection from './components/courseSection.vue'

export default {
  components: { courseSection },
  props: {
    courseId: {
      type: [Number, String],
      require: true
    }
  },
  data () {
    return {
      course: {},
      sections: [],
      styleOptions: {}
    }
  },
  methods: {
    async loadCourse () {
      const { data } = await getCourseById({ courseId: this.courseId })
      this.course = data.content
      if (data.content.isBuy) {
        this.styleOptions.bottom = 0
      }
    },
    async loadSectionLesson () {
      const { data } = await getSectionAndLesson({ courseId: this.courseId })
      this.sections = data.content.courseSectionList
    },
    handlePay () {
      // 检测是否登录
      if (this.$store.state.user) {
        // 如果已登录
        this.$router.push({
          name: 'Pay',
          params: {
            courseId: this.courseId
          }
        })
      } else {
        this.$router.push({
          name: 'Login',
          query: {
            redirect: this.$route.fullPath
          }
        })
      }
    }
  },
  created () {
    this.loadCourse()
    this.loadSectionLesson()
  }
}
</script>

<style scoped>
.van-cell{
  padding: 0;
}
.course-img{
  height: 280px;
}
.course-img img{
  height: 280px;
  width: 100%;
}
/* 课程描述区域 */
.course-description{
    padding: 10px 20px;
    height: 150px;
}
.course-description h2{
    margin: 0;
}
.course-sale-info {
    display: flex;
}
.course-sale-info .course-price{
    flex: 1;
    margin: 0;
}
.discounts{
    color: #ff7452;
    font-size: 24px !important;
    font-weight: 700;
}
.course-sale-info .tag{
    background: #f8f9fa;
    font-size: 12px;
    font-weight: 700;
    color: #666;
    margin-left: 7px;
    padding: 0 4px;
    line-height: 30px;
    border-radius: 4px;
}
/* 添加底部导航后设置 */
.van-cell-group{
  width: 100%;
  position: fixed;
  top: 0;
  bottom: 50px;
  overflow-y: scroll;
}
.van-tabbar{
  line-height: 50px;
  padding: 0 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.price span{
  font-size: 14px;
}
.van-button{
  width: 50%;
  height: 80%;
}
</style>
